package com.handinglian.task.service.impl;

import com.handinglian.common.utils.FastJsonUtil;
import com.handinglian.servicerecord.entity.CallRecord;
import com.handinglian.servicerecord.service.CallRecordService;
import com.handinglian.task.dto.AssignedNumDto;
import com.handinglian.task.dto.CustomerTaskDto;
import com.handinglian.task.entity.TaskUserRelation;
import com.handinglian.task.enums.CalllStateEnum;
import com.handinglian.task.mapper.TaskUserRelationMapper;
import com.handinglian.task.param.CustomerIntentionUpdateParam;
import com.handinglian.task.param.TaskUserRelationUpdateParam;
import com.handinglian.task.param.TaskUserTopParam;
import com.handinglian.task.service.TaskUserRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service("taskUserRelationService")
public class TaskUserRelationServiceImpl implements TaskUserRelationService {
    @Autowired
    private TaskUserRelationMapper taskUserRelationMapper;
    @Autowired
    private CallRecordService callRecordService;

    @Override
    @Transactional
    public int deleteTaskUserByTaskId(Integer taskId) {
        return taskUserRelationMapper.deleteByPrimaryKey(taskId);
    }

    @Override
    public List<TaskUserRelation> inquireTaskUserByTaskId(Integer taskId) {
        return taskUserRelationMapper.findByTaskId(taskId);
    }

    @Override
    public List<TaskUserRelation> list(Integer taskId, Integer userId) {
        return taskUserRelationMapper.findByTaskIdAndUserId(taskId, userId);
    }

    @Override
    public List<TaskUserRelation> list(List<Integer> taskIds, Integer userId) {
        return taskUserRelationMapper.findByTaskIdInAndUserId(taskIds, userId);
    }

    @Override
    public TaskUserRelation loadTaskUser(Integer taskUserId) {
        return taskUserRelationMapper.selectByPrimaryKey(taskUserId);
    }

    @Override
    @Transactional
    public int createTaskUserRelationBatch (List<TaskUserRelation> list) {
        return taskUserRelationMapper.batchInsert(list);
    }

    @Override
    @Transactional
    public int updateCustomerIntention(List<CustomerIntentionUpdateParam> customerIntentionUpdateParams) {
        List<TaskUserRelation> taskUserRelationList = FastJsonUtil.ListToList(customerIntentionUpdateParams, TaskUserRelation.class);
        int count = 0;
        for (TaskUserRelation taskUserRelation : taskUserRelationList){
            taskUserRelationMapper.updateByPrimaryKeySelective(taskUserRelation);
            count++;
        }
        return count;
    }

    @Override
    @Transactional
    public int updateTaskUserRelation(TaskUserRelationUpdateParam taskUserRelationUpdateParam) {
        TaskUserRelation taskUserRelation = FastJsonUtil.ObjectToObject(taskUserRelationUpdateParam, TaskUserRelation.class);
        return taskUserRelationMapper.updateByPrimaryKeySelective(taskUserRelation);
    }

    @Override
    public List<CustomerTaskDto> inquireCustomerTaskList(Integer userId, String customerPhone) {
        List<CustomerTaskDto> customerTaskDtos = taskUserRelationMapper.inquireCustomerTaskList(userId, customerPhone);
        customerTaskDtos = customerTaskDtos.stream().filter(customerTaskDto -> System.currentTimeMillis() <= customerTaskDto.getEndDate().getTime()).collect(Collectors.toList());

        List<CallRecord> callRecords = callRecordService.inquireCallRecordByCustomerNo(userId, customerPhone);
        List<CallRecord> answerRecords = callRecordService.inquireAnswerRecordByCustomerNo(userId, customerPhone);

        //设置通话状态
        for (CustomerTaskDto customerTaskDto : customerTaskDtos){
            List<CallRecord> callRecordsSub = callRecords.stream().filter(callRecord -> callRecord.getUserId().equals(customerTaskDto.getUserId())).collect(Collectors.toList());
            if (callRecordsSub.size() == 0){
                customerTaskDto.setCallState(CalllStateEnum.WAIT_TO_CALL.getKey());
            } else {
                List<CallRecord> answerRecordSub = answerRecords.stream().filter(answerRecord -> answerRecord.getUserId().equals(customerTaskDto.getUserId())).collect(Collectors.toList());
                if (answerRecordSub.size() == 0){
                    customerTaskDto.setCallState(CalllStateEnum.NOT_ANSWER.getKey());
                } else {
                    customerTaskDto.setCallState(CalllStateEnum.ANSWER.getKey());
                }
            }
        }
        return customerTaskDtos;
    }

    @Override
    public List<AssignedNumDto> inquireAssignedNumList() {
        return taskUserRelationMapper.inquireAssignedNumList();
    }

    @Override
    @Transactional
    public int delete(Integer taskUserId) {
        return taskUserRelationMapper.deleteByPrimaryKey(taskUserId);
    }

    @Override
    public List<TaskUserRelation> list(Integer userId) {
        return taskUserRelationMapper.findByUserId(userId);
    }

    @Override
    @Transactional
    public int updateTop(TaskUserTopParam taskUserTopParam) {
        TaskUserRelation taskUserRelation = new TaskUserRelation();
        taskUserRelation.setTaskUserId(taskUserTopParam.getTaskUserId());
        taskUserRelation.setTop(taskUserTopParam.getTop());
        return taskUserRelationMapper.updateByPrimaryKeySelective(taskUserRelation);
    }

    @Override
    @Transactional
    public int updateValid(Integer userId) {
        return taskUserRelationMapper.updateValidByUserId(0, userId);
    }

    @Override
    public List<Integer> getTaskId() {
        return taskUserRelationMapper.findDistinctTaskId();
    }

    @Override
    public List<String> listCustomerPhone(Integer taskId, Collection<String> customerPhoneCollection) {
        return taskUserRelationMapper.findCustomerPhoneByTaskIdAndCustomerPhoneIn(taskId, customerPhoneCollection);
    }

    @Override
    public List<TaskUserRelation> list(Integer taskId, List<String> customerPhones) {
        return taskUserRelationMapper.findByTaskIdAndCustomerPhoneIn(taskId, customerPhones);
    }

    @Override
    @Transactional
    public int updateBatch(List<TaskUserRelation> list) {
        return taskUserRelationMapper.updateBatch(list);
    }
}
